cc.Class({
  extends: cc.Component,
  properties: {
    loadingBar: cc.ProgressBar
  },
  onLoad: function () {
    cc.butler = this;
    this.dFlag = 0;
    cc.pvz.time = 0;
    cc.director.on(cc.Director.EVENT_BEFORE_UPDATE, this.beforeDirectorUpdate, this);
    cc.sys.platform == cc.sys.WECHAT_GAME && this.polyfillSafeArea();
  },
  polyfillSafeArea: function () {
    if (!wx.getSystemInfoSync().safeArea) {
      console.log("polyfill safeArea");
      cc.sys.getSafeAreaRect = function () {
        var e = cc.view.getVisibleSize();
        return cc.rect(0, 0, e.width, e.height);
      };
    }
  },
  beforeDirectorUpdate: function () {
    this.checkToHandleGameHide && this.handleGameHide();
    var e = 1e3 * cc.director.getDeltaTime();
    cc.pvz.time += e * cc.director.getScheduler().getTimeScale();
    if (cc.pvz.cloud && cc.pvz.cloud.uid && cc.pvz.cloud.needSaveToCloud > 0) {
      cc.pvz.PlayerData.saveDataToLocalOnline();
      cc.pvz.cloud.needSaveToCloud = 0;
    }
  },
  start: function () {
    var e = this;
    cc.game.addPersistRootNode(this.node);
    if (cc.sys.platform == cc.sys.WECHAT_GAME) {
      wx.onHide(function () {
        console.log("wx.onHide");
        e.handleGameHide();
      });
      wx.onShow(function () {
        console.log("wx.onShow");
        e.handleGameShow();
        cc.pvz.TAUtils.onWxShow();
      });
    } else if (cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
      tt.onHide(function () {
        console.log("tt.onHide");
        e.handleGameHide();
      });
      tt.onShow(function () {
        console.log("tt.onShow");
        e.handleGameShow();
      });
    } else {
      cc.game.on(cc.game.EVENT_HIDE, function () {
        console.log("cc.game.EVENT_HIDE, frame:", cc.director.getTotalFrames());
        e.handleGameHide();
      });
      cc.game.on(cc.game.EVENT_SHOW, function () {
        console.log("cc.game.EVENT_SHOW, frame:", cc.director.getTotalFrames());
        e.handleGameShow();
      });
    }
    cc.pvz.TAUtils.onCheckGameVersion();
  },
  handleGameHide: function () {
    console.log("handleGameHide");
    this.pauseMusic();
    cc.audioEngine.stopAllEffects();
    cc.isRestart || this.saveData();
  },
  handleGameShow: function () {
    this.resumeMusic();
  },
  pauseDirector: function (e) {
    0 == this.dFlag && cc.director.pause();
    this.dFlag |= 1 << e;
  },
  resumeDirector: function (e) {
    this.dFlag &= ~(1 << e);
    0 == this.dFlag && cc.director.resume();
  },
  playMusic: function (e) {
    this.music = e;
    if (!(cc.player && cc.player.isMMute)) {
      this.playingMusic = e;
      if (e) {
        console.log("playmusic"), cc.audioEngine.playMusic(e, true);
      }
    }
  },
  pauseMusic: function () {
    cc.audioEngine.pauseMusic();
  },
  resumeMusic: function () {
    if (cc.player && cc.player.isMMute) {
      console.log("resumeMusic return");
    } else if (this.music != this.playingMusic) {
      console.log("resumeMusic play new");
      this.playMusic(this.music);
    } else {
      console.log("resumeMusic resume old");
      cc.audioEngine.resumeMusic();
    }
  },
  playEffect: function (e, t) {
    undefined === t && (t = false);
    if (cc.player && cc.player.isSMute) {
      return -1;
    } else {
      if (e) {
        return cc.audioEngine.playEffect(e, t);
      } else {
        return -1;
      }
    }
  },
  resumeEffect: function (e) {
    if (cc.player && cc.player.isSMute) {
      return -1;
    }
    cc.audioEngine.resumeEffect(e);
  },
  playEffectAsync: function (e, t, o) {
    var a = this;
    undefined === o && (o = false);
    if (!cc.player || !cc.player.isSMute) {
      var n = null;
      if (o) {
        this.exclusiveMap || (this.exclusiveMap = {});
        n = e + t;
        if (this.exclusiveMap[n]) {
          return;
        }
        this.exclusiveMap[n] = true;
      }
      cc.pvz.utils.useBundleAsset(e, t, cc.AudioClip, function (e) {
        if (o) {
          var t = a.playEffect(e, false);
          if (-1 == t) {
            a.exclusiveMap[n] = false;
          } else {
            cc.audioEngine.setFinishCallback(t, function () {
              a.exclusiveMap[n] = false;
            });
          }
        } else {
          a.playEffect(e, false);
        }
      });
    }
  },
  playEffectAsync2: function (e) {
    if (!cc.player || !cc.player.isSMute) {
      var t = e.indexOf(",");
      this.playEffectAsync(e.substring(0, t), e.substring(t + 1));
    }
  },
  setMusicSwitch: function (e) {
    cc.player.isMMute != e && this.node.emit("music-switch", e);
    this.setMusicMute(e);
    if (cc.player.isMMute) {
      this.pauseMusic();
    } else {
      this.resumeMusic();
    }
  },
  setSoundSwitch: function (e) {
    this.setSoundMute(e);
    cc.player.isSMute && cc.audioEngine.stopAllEffects();
  },
  saveData: function () {
    cc.pvz.PlayerData.saveData();
  },
  setMusicMute: function (e) {
    cc.player.isMMute = e;
    cc.pvz.PlayerData.onDataChanged();
  },
  setSoundMute: function (e) {
    cc.player.isSMute = e;
    cc.pvz.PlayerData.onDataChanged();
  },
  onToggleMusic: function (e) {
    console.log("onToggleMusic", e.isChecked);
    this.setMusicSwitch(!e.isChecked);
    this.setSoundSwitch(!e.isChecked);
  },
  loadBundles: function (e, t, o) {
    var a = this;
    this.loadingBar && (this.loadingBar.progress = 0);
    this.loadedBundle = 0;
    cc.assetManager.loadBundle(e[0], null, function n(i, c) {
      if (i) {
        console.log("load subpackage  fail:", i);
      } else {
        console.log("load bundle " + c.name + " successfully.");
        a.loadedBundle++;
        if (a.loadedBundle == e.length) {
          o && o();
        } else {
          cc.assetManager.loadBundle(e[a.loadedBundle], null, n);
        }
        a.loadingBar && cc.tween(a.loadingBar).to(.2, {
          progress: (a.loadedBundle + 1) / t
        }).start();
      }
    });
    this.loadingBar && cc.tween(this.loadingBar).to(.2, {
      progress: (this.loadedBundle + 1) / t
    }).start();
  }
});